import { sendMessage } from "../message"
import type RoomData from "../roomData"
import type requestMessage from "./requestMessage"
import type responseMessage from "./responseMessage"
import websocket from "./websocket"

export default interface connection {
    name: string
    room: string
    sendMessage: (massage: requestMessage) => boolean
    open: () => Promise<void>
    close: () => boolean
    handle: (message: responseMessage) => void
    disconnected: (connection: connection) => void
    workable: boolean
}
export type connectSetting = { type: string, room: string, userName: string, enterPassword: string, password: string }
export function getConnection(setting: connectSetting) {
    if (setting.room == '' || setting.room == undefined) {
        sendMessage( '房间名不能为空',"warn")
        return;
    }
    if (setting.userName == '' || setting.userName == undefined) {
        sendMessage( '用户名不能为空',"warn")
        return;
    }
    switch (setting.type) {
        case 'websocket': {
            return new websocket(setting.room, setting.userName, setting.enterPassword)
        }
        default: {
            return new websocket(setting.room, setting.userName, setting.enterPassword)
        }
    }
}
export function checkConnectSetting(obj:any):connectSetting{
    return{
        type:obj?.type?.toString()??'',
        room:obj?.room?.toString()??'',
        userName:obj?.userName?.toString()??'',
        enterPassword:obj?.enterPassword?.toString()??'',
        password:obj?.password?.toString()??'',
    }
}